package org.android.cosmus.indiam.activities;

import org.android.cosmus.indiam.R;
import org.android.cosmus.indiam.utilities.Utils;
import org.android.cosmus.indiam.views.CustomArrayAdapter;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;

public class TutorialActivity extends ListActivity
{
  
  private String[] m_listItens;
  
  @Override
  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.list_view);
    int itemId = this.getIntent().getIntExtra("item_description", R.array.root_node);
    m_listItens = this.getResources().getStringArray(itemId);
    CustomArrayAdapter adapter = new CustomArrayAdapter(this, m_listItens);
    setListAdapter(adapter);
    registerForContextMenu(this.getListView());
  }

  @Override
  protected void onListItemClick(ListView l, View v, int position, long id)
  {
    super.onListItemClick(l, v, position, id);
    
    int resourceId = Utils.getResourceArrayIdBasedOnName(this, m_listItens[position], "_node");
    if(resourceId != 0)
    {
      Intent intent = new Intent("org.android.cosmus.indiam.activities.TUTORIAL");
      intent.putExtra("item_description", resourceId);
      startActivity(intent);
    }
    else
    {
      //TODO: Criar task que ira carregar pagina com informacoes detalhadas
    }
  }

  @Override
  public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo)
  {
    menu.setHeaderTitle(R.string.app_name);
    menu.add(Menu.NONE, 0, 0, R.string.brief);
    menu.add(Menu.NONE, 1, 1, R.string.details);
  }
  
  @Override
  public boolean onContextItemSelected(MenuItem item)
  {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    int position = info.position;
    switch(position)
    {
      case 0:
        String itemString = m_listItens[position];
        int briefItemId = Utils.getResourceStringIdBasedOnName(this, itemString, "_brief");
        Utils.makeAlertDialog(this, itemString, this.getString(briefItemId));
        break;
      case 1:
        //TODO: Criar task que ira carregar pagina com informacoes detalhadas
        break;
    }
    return true;
  }
}
